let nowChess = null;
let nowChessId = null;
//记录当前图形的旋转角度
let nowChessShape = 0;
//是否经过水平翻转
let isLevelChange = false;
//是否经过垂直翻转
let isUprightChange = false;
var chessPosition = createChessPosition();
let nowChessPosition;
let firstChess = true;

function createChessPosition() {
	let arrRoot = new Array(20);
	for(let i = 0; i < 20; i++) {
		let arr = new Array(20);
		for(let j = 0; j < 20; j++) {
			arr[j] = 0;
		}
		arrRoot[i] = arr;
	}
	return arrRoot;
}

function pass() {
	if(nowChess != null) {
		$("#"+nowChess).remove();
		$("#"+nowChessId).show();
		document.onkeydown = null;
		nowChess = null;
	}
	sendPassMsg();
	otherTurn();
}

function chooseChess(event) {
	if(nowChess != null) {
		myAlert("warning", "操作违规");
		return;
	}
	if(!isMyTurn) {
		myAlert("warning", "当前非你的回合，请等待")
		return;
	}

	//初始化棋子位置信息
	isLevelChange = false;
	isUprightChange = false;
	nowChessShape = 0;

	let id = getEvent(event).target.id;
	nowChessId = id;
	let addChess = "<div id='" + id + "_down' class='chess_down' style='transform-origin: 0% 0%;transition: transform 0.5s ease-in-out;"
		+ createChess[id].css +"' onmousedown='moveAction(event)'></div>";
	$(".chess_board").append(addChess);
	$("#"+id).hide();
	$("#"+id+"_down").css("margin-top","0px");
	nowChess = id + "_down";
	document.onkeydown = function(event) {
		let dom = document.getElementById(nowChess);
		if(event.key === "Escape") {
			$("#"+nowChess).remove();
			$("#"+nowChessId).show();
			document.onkeydown = null;
			nowChess = null;
		} else if(event.key === "w" || event.key === "ArrowUp") {
			if(isLevelChange) {
				isLevelChange = false;
				if(isUprightChange) {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg) scale(1,-1)';
				} else {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg)';
				}
			} else {
				isLevelChange = true;
				if(isUprightChange) {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg) scale(-1,-1)';
				} else {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg) scale(-1,1)';
				}
			}
		} else if(event.key === "s" || event.key === "ArrowDown") {
			if(isUprightChange) {
				isUprightChange = false;
				if(isLevelChange) {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg) scale(-1,1)';
				} else {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg)';
				}
			} else {
				isUprightChange = true;
				if(isLevelChange) {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg) scale(-1,-1)';
				} else {
					dom.style.transform = 'rotate(' + nowChessShape + 'deg) scale(1,-1)';
				}
			}
		} else if(event.key === "a" || event.key === "ArrowLeft") {
			nowChessShape += 90;
			relocation(dom);
		} else if(event.key === "d" || event.key === "ArrowRight") {
			nowChessShape -= 90;
			relocation(dom);
		}
	}
}

function relocation(dom) {
	if(isUprightChange && isLevelChange) {
		dom.style.transform = "rotate(" + nowChessShape + "deg) scale(-1, -1)";
	} else if (isLevelChange) {
		dom.style.transform = "rotate(" + nowChessShape + "deg) scale(-1, 1)";
	} else if(isUprightChange) {
		dom.style.transform = "rotate(" + nowChessShape + "deg) scale(1, -1)";
	} else {
		dom.style.transform = "rotate(" + nowChessShape + "deg)";
	}
}

function confirm() {
	//offsetLeft: 21   offsetTop: 21
	let dom = $("#"+nowChess)[0];
	let left = dom.offsetLeft - 21;
	let top = dom.offsetTop - 21;
	if(left < 0 || top < 0) {
		myAlert("error","落子位置违规！");
		return;
	}
	left = left / 35;
	top = top / 35;
	nowChessPosition = createChessPosition();
	let result = checkChessAround(top, left);
	if(!result) {
		myAlert("error","落子位置违规！");
		return;
	}
	if(firstChess) {
		result = checkFirstChess(top, left);
	} else {
		result = checkChessCorner(top, left);
	}
	if(!result) {
		myAlert("error","落子位置违规！");
		return;
	}
	firstChess = false;
	setChessDown(top, left);
	$("#"+nowChess).attr("onmousedown",null);

	let message = {
		row : top,
		line : left,
		chess: nowChess,
		gameState: "running",
		isUprightChange : isUprightChange,
		isLevelChange : isLevelChange,
		nowChessShape : nowChessShape
	}
	document.onkeydown = null;
	nowChess = null;
	positionConfirm(message);
}

function errorSupport() {
	if(nowChess == null) {
		myAlert("warning", "请先选择棋子")
		return;
	}

	let errorMessage = {};
	let dom = $("#"+nowChess)[0];
	let left = dom.offsetLeft - 21;
	let top = dom.offsetTop - 21;
	if(left < 0 || top < 0 || firstChess) {
		myAlert("warning", "当前不支持提交错误")
		return;
	}
	left = left / 35;
	top = top / 35;
	nowChessPosition = createChessPosition();
	let result = checkChessAround(top, left);
	errorMessage.row = top;
	errorMessage.line = left;
	errorMessage.checkChessAround = result;
	errorMessage.nowChessPosition = nowChessPosition;
	errorMessage.allChessPosition = chessPosition;
	errorMessage.nowChess = nowChess;
	errorMessage.chessPosition = dealShapeChange();
	errorMessage.isUprightChange = isUprightChange;
	errorMessage.isLevelChange = isLevelChange;
	errorMessage.nowChessShape = nowChessShape;

	result = checkChessCorner(top, left);
	errorMessage.checkChessCorner = result;
	let message = {
		errorMessage: JSON.stringify(errorMessage),
		gameState: "error"
	}
	sendMessage(message);
	myAlert("success", "提交成功，请等待管理员处理");
}

function dealShapeChange() {
	let obj = createChessDown[nowChess];
	obj = JSON.parse(JSON.stringify(obj));
	let soonNowChessShape = nowChessShape;
	if(isLevelChange && isUprightChange) {
		soonNowChessShape += 180;
	} else if(isLevelChange) {
		obj.positionY = -obj.positionY;
		let soon = obj.left;
		obj.left = obj.right;
		obj.right = soon;
		soon = obj.left_top;
		obj.left_top = obj.right_top;
		obj.right_top = soon;
		soon = obj.left_bottom;
		obj.left_bottom = obj.right_bottom;
		obj.right_bottom = soon;

		obj.width = -obj.width;
	} else if(isUprightChange) {
		obj.positionX = - obj.positionX;
		let soon = obj.top;
		obj.top = obj.bottom;
		obj.bottom = soon;
		soon = obj.left_top;
		obj.left_top = obj.left_bottom;
		obj.left_bottom = soon;
		soon = obj.right_top;
		obj.right_top = obj.right_bottom;
		obj.right_bottom = soon;

		obj.height = -obj.height;
	}
	soonNowChessShape = soonNowChessShape%360;
	soonNowChessShape = soonNowChessShape/90;
	let flag = true;
	if(soonNowChessShape < 0) {
		soonNowChessShape = -soonNowChessShape;
		flag = false;
	}
	for(let i = 0; i < soonNowChessShape; i++) {
		shapeChange(obj, flag);
	}
	if(obj.positionX > 0) {
		obj.positionX -= 1;
	}
	if(obj.positionY > 0) {
		obj.positionY -= 1;
	}
	return obj;
}

function dealShapeChangeWithParam(message) {
	let obj = createChessDown[message.chess];
	obj = JSON.parse(JSON.stringify(obj));
	let soonNowChessShape = message.nowChessShape;
	if(message.levelChange && message.uprightChange) {
		soonNowChessShape += 180;
	} else if(message.levelChange) {
		obj.positionY = -obj.positionY;
		let soon = obj.left;
		obj.left = obj.right;
		obj.right = soon;
		soon = obj.left_top;
		obj.left_top = obj.right_top;
		obj.right_top = soon;
		soon = obj.left_bottom;
		obj.left_bottom = obj.right_bottom;
		obj.right_bottom = soon;

		obj.width = -obj.width;
	} else if(message.uprightChange) {
		obj.positionX = - obj.positionX;
		let soon = obj.top;
		obj.top = obj.bottom;
		obj.bottom = soon;
		soon = obj.left_top;
		obj.left_top = obj.left_bottom;
		obj.left_bottom = soon;
		soon = obj.right_top;
		obj.right_top = obj.right_bottom;
		obj.right_bottom = soon;

		obj.height = -obj.height;
	}
	soonNowChessShape = soonNowChessShape%360;
	soonNowChessShape = soonNowChessShape/90;
	let flag = true;
	if(soonNowChessShape < 0) {
		soonNowChessShape = -soonNowChessShape;
		flag = false;
	}
	for(let i = 0; i < soonNowChessShape; i++) {
		shapeChange(obj, flag);
	}
	if(obj.positionX > 0) {
		obj.positionX -= 1;
	}
	if(obj.positionY > 0) {
		obj.positionY -= 1;
	}
	return obj;
}

//flag为true表示顺时针旋转
function shapeChange(obj,flag) {
	let soon;
	if(flag) {
		soon = obj.positionY;
		obj.positionY = -obj.positionX;
		obj.positionX = soon;

		soon = obj.height;
		obj.height = obj.width;
		obj.width = -soon;

		soon = obj.top;
		obj.top = obj.left;
		obj.left = obj.bottom;
		obj.bottom = obj.right;
		obj.right = soon;

		soon = obj.left_top;
		obj.left_top = obj.left_bottom;
		obj.left_bottom = obj.right_bottom;
		obj.right_bottom = obj.right_top;
		obj.right_top = soon;
	} else {
		soon = obj.positionX;
		obj.positionX = -obj.positionY;
		obj.positionY = soon;

		soon = obj.width;
		obj.width = obj.height;
		obj.height = -soon;

		soon = obj.top;
		obj.top = obj.right;
		obj.right = obj.bottom;
		obj.bottom = obj.left;
		obj.left = soon;

		soon = obj.left_top;
		obj.left_top = obj.right_top;
		obj.right_top = obj.right_bottom;
		obj.right_bottom = obj.left_bottom;
		obj.left_bottom = soon;
	}
}

function checkFirstChess(row, line) {
	let obj = dealShapeChange();
	let width = obj.width;
	let height = obj.height;

	if(width < 0) {
		line += width;
		width = -width;
	}
	if(height < 0) {
		row += height;
		height = -height;
	}
	height -= 1;
	width -= 1;
	if(row === 0 && line === 0) {
		return true;
	} else if(row+height === 19 && line === 0) {
		return true;
	} else if(row + height === 19 && line + width === 19) {
		return true;
	} else if(row === 0 && line + width === 19) {
		return true;
	}
	return false;
}

function setChessDown(row, line) {
	let obj = dealShapeChange();
	let x = row + obj.positionX;
	let y = line + obj.positionY;
	chessPosition[x][y] = 1;
	let sum = obj.left;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x][y-i] = 1;
	}
	sum = obj.right;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x][y+i] = 1;
	}
	sum = obj.top;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x-i][y] = 1;
	}
	sum = obj.bottom;
	for(let i = 1; i <= sum; i++) {
		chessPosition[x+i][y] = 1;
	}
	sum = obj.left_top;
	if(sum === 1) {
		chessPosition[x-1][y-1] = 1;
	}
	sum = obj.left_bottom;
	if(sum === 1) {
		chessPosition[x+1][y-1] = 1;
	}
	sum = obj.right_bottom;
	if(sum === 1) {
		chessPosition[x+1][y+1] = 1;
	}
	sum = obj.right_top;
	if(sum === 1) {
		chessPosition[x-1][y+1] = 1;
	}
}

function checkChessCorner(row, line) {
	let obj = dealShapeChange();
	let x = row + obj.positionX;
	let y = line + obj.positionY;
	let result = checkCorner(x,y);
	if(result) {
		return true;
	}
	let sum = obj.left;
	for(let i = 1; i <= sum; i++) {
		result = checkCorner(x,y-i);
		if(result) {
			return true;
		}
	}
	sum = obj.right;
	for(let i = 1; i <= sum; i++) {
		result = checkCorner(x,y+i);
		if(result) {
			return true;
		}
	}
	sum = obj.top;
	for(let i = 1; i <= sum; i++) {
		result = checkCorner(x-i,y);
		if(result) {
			return true;
		}
	}
	sum = obj.bottom;
	for(let i = 1; i <= sum; i++) {
		result = checkCorner(x+i,y);
		if(result) {
			return true;
		}
	}
	sum = obj.left_top;
	if(sum === 1) {
		result = checkCorner(x-1,y-1);
		if(result) {
			return true;
		}
	}
	sum = obj.left_bottom;
	if(sum === 1) {
		result = checkCorner(x+1,y-1);
		if(result) {
			return true;
		}
	}
	sum = obj.right_bottom;
	if(sum === 1) {
		result = checkCorner(x+1,y+1);
		if(result) {
			return true;
		}
	}
	sum = obj.right_top;
	if(sum === 1) {
		result = checkCorner(x-1,y+1);
		if(result) {
			return true;
		}
	}
	return false;
}

function checkChessAround(row, line) {
	let obj = dealShapeChange();
	let x = row + obj.positionX;
	let y = line + obj.positionY;
	let result = checkAround(x,y);
	if(!result) {
		return false;
	}
	let sum = obj.left;
	for(let i = 1; i <= sum; i++) {
		result = checkAround(x,y-i);
		if(!result) {
			return false;
		}
	}
	sum = obj.right;
	for(let i = 1; i <= sum; i++) {
		result = checkAround(x,y+i);
		if(!result) {
			return false;
		}
	}
	sum = obj.top;
	for(let i = 1; i <= sum; i++) {
		result = checkAround(x-i,y);
		if(!result) {
			return false;
		}
	}
	sum = obj.bottom;
	for(let i = 1; i <= sum; i++) {
		result = checkAround(x+i,y);
		if(!result) {
			return false;
		}
	}
	sum = obj.left_top;
	if(sum === 1) {
		result = checkAround(x-1,y-1);
		if(!result) {
			return false;
		}
	}
	sum = obj.left_bottom;
	if(sum === 1) {
		result = checkAround(x+1,y-1);
		if(!result) {
			return false;
		}
	}
	sum = obj.right_bottom;
	if(sum === 1) {
		result = checkAround(x+1,y+1);
		if(!result) {
			return false;
		}
	}
	sum = obj.right_top;
	if(sum === 1) {
		result = checkAround(x-1,y+1);
		if(!result) {
			return false;
		}
	}
	return true;
}

function checkAround(row, line) {
	if(row < 0 || line < 0 || row > 19 || line > 19 || chessPosition[row][line] != 0) {
		return false;
	}
	if(row - 1 >= 0 && chessPosition[row-1][line] != 1) {
		nowChessPosition[row-1][line] = 1;
	} else if(row - 1 >= 0){
		return false;
	}
	if(row + 1 < 20 && chessPosition[row+1][line] != 1) {
		nowChessPosition[row+1][line] = 1;
	} else if(row + 1 < 20) {
		return false;
	}
	if(line - 1 >= 0 && chessPosition[row][line-1] != 1) {
		nowChessPosition[row][line-1] = 1;
	} else if(line - 1 >= 0){
		return false;
	}
	if(line + 1 < 20 && chessPosition[row][line+1] != 1) {
		nowChessPosition[row][line+1] = 1;
	} else if(line + 1 < 20){
		return false;
	}
	return true;
}

function checkCorner(row, line) {
	if(row - 1 >= 0 && line - 1 >= 0) {
		if(nowChessPosition[row-1][line-1] != 1 && chessPosition[row-1][line-1] == 1) {
			return true;
		}
	}
	if(row - 1 >= 0 && line + 1 < 20) {
		if(nowChessPosition[row-1][line+1] != 1 && chessPosition[row-1][line+1] == 1) {
			return true;
		}
	}
	if(row + 1 < 20 && line + 1 < 20) {
		if(nowChessPosition[row+1][line+1] != 1 && chessPosition[row+1][line+1] == 1) {
			return true;
		}
	}
	if(row + 1 < 20 && line - 1 >= 0) {
		if(nowChessPosition[row+1][line-1] != 1 && chessPosition[row+1][line-1] == 1) {
			return true;
		}
	}
	return false;
}
